// pages/shopStore/search/index.js
const app = getApp()
const util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    parameter: {
      'navbar': '1',
      'return': '1',
      'title': '店铺搜索',
      'color': true,
      'class': false
    },
    makeType: 1,
    shopHistory: [], //店铺历史记录
    commodityHistory: [] //商品历史记录
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.manageItem != 1){
      this.setData({
        'parameter.title': '商品搜素',
        makeType: options.manageItem,
        shopHistory : wx.getStorageSync('shopHistory') || [],
        commodityHistory : wx.getStorageSync('commodityHistory') || []
      })
    }else {
      this.setData({
        'parameter.title': '店铺搜素',
        makeType: options.manageItem,
        shopHistory : wx.getStorageSync('shopHistory') || [],
        commodityHistory : wx.getStorageSync('commodityHistory') || []
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /* 
    点击搜搜历史
  */
  handleSearch(e){
    wx.setStorageSync('name', e.currentTarget.dataset.item)
    wx.setStorageSync('makeType', this.data.makeType)
    wx.switchTab({
      url: "/pages/shop/index"
    })
  },
  /* 点击键盘的搜搜 */
  bindconfirm(e){
    if(e.detail.value == ' ' || e.detail.value == ''){
      return util.Tips({ title: '请输入要搜索的内容' })
    }
    if(this.data.makeType == 1){
      var history = this.data.shopHistory;
      history.push(e.detail.value)
      wx.setStorageSync('shopHistory', history)
      wx.setStorageSync('name', e.detail.value)
      wx.setStorageSync('makeType', this.data.makeType)
      this.setData({
        shopHistory: history
      })
    }else {
      var history = this.data.commodityHistory;
      history.push(e.detail.value)
      wx.setStorageSync('commodityHistory', history)
      wx.setStorageSync('name', e.detail.value)
      wx.setStorageSync('makeType', this.data.makeType)
      this.setData({
        commodityHistory: history
      })
    }
    wx.switchTab({
      url: "/pages/shop/index" 
    })
  },

  /* 删除历史记录 */
  clearSearch(){
    if(this.data.makeType == 1){
      wx.removeStorageSync('shopHistory')
      this.setData({
        shopHistory: []
      })
    }else {
      wx.removeStorageSync('commodityHistory')
      this.setData({
        commodityHistory: []
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})